package com.dhq.myapp.util.dialog

import android.content.Context
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import androidx.annotation.LayoutRes
import com.dhq.myapp.R
import com.dhq.myapp.enum.DateType
import com.dhq.myapp.util.dialog.custom.CustomBottomPopup
import com.dhq.myapp.util.dialog.custom.CustomPopup
import com.dhq.myapp.util.dialog.custom.DefaultCenterPopup
import com.dhq.myapp.util.dialog.custom.DefaultLoadingPopup
import com.dhq.myapp.util.dialog.listener.DateListener
import com.dhq.myapp.util.dialog.listener.DialogDropDownListener
import com.dhq.myapp.util.dialog.listener.DialogListSelectListener
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.enums.PopupPosition

import com.lxj.xpopup.interfaces.OnSelectListener

import java.util.Calendar
import java.util.Date


/**
 * DESC 弹框工具类
 *
 * @author admin
 * @date 2019/2/26
 */
class DialogUtils(builder: DialogBuilder) {
    var mBuilder: DialogBuilder

    init {
        this.mBuilder = builder
    }

    /**
     * 显示屏幕中间提示框
     *
     * @return
     */
    fun showCenterDialog(): BasePopupView {
        if (mBuilder.layoutId !== 0) {
            return showCustomCenterDialog()
        }

        return XPopup.Builder(mBuilder.context)
            .popupAnimation(if (mBuilder.popupAnimation == null) PopupAnimation.ScaleAlphaFromCenter else mBuilder.popupAnimation)
            .autoOpenSoftInput(mBuilder.isAutoOpenSoftInput)
            .dismissOnTouchOutside(mBuilder.isCanDismiss)
            .dismissOnBackPressed(mBuilder.isBackPressedDismiss)
            .asCustom(DefaultCenterPopup(mBuilder))
            .show()
    }


    /**
     * 显示屏幕中间等待框
     *
     * @return
     */
    fun showLoadingDialog(): BasePopupView {
        return XPopup.Builder(mBuilder.context)
            .popupAnimation(if (mBuilder.popupAnimation == null) PopupAnimation.ScaleAlphaFromCenter else mBuilder.popupAnimation)
            .autoOpenSoftInput(mBuilder.isAutoOpenSoftInput)
            .dismissOnTouchOutside(mBuilder.isCanDismiss)
            .dismissOnBackPressed(mBuilder.isBackPressedDismiss)
            .asCustom(DefaultLoadingPopup(mBuilder))
            .show()
    }


    /**
     * 显示自定义屏幕中间弹框
     * @return
     */
    fun showCustomCenterDialog(): BasePopupView {
        return XPopup.Builder(mBuilder.context)
            .popupAnimation(if (mBuilder.popupAnimation == null) PopupAnimation.ScaleAlphaFromCenter else mBuilder.popupAnimation)
            .autoOpenSoftInput(mBuilder.isAutoOpenSoftInput)
            .dismissOnTouchOutside(mBuilder.isCanDismiss)
            .dismissOnBackPressed(mBuilder.isBackPressedDismiss)
            .asCustom(CustomPopup(mBuilder))
            .show()
    }

    /**
     * 显示自定义屏幕底部弹框
     * @return
     */
    fun showBottomDialog(): BasePopupView {
        return XPopup.Builder(mBuilder.context) //                .popupAnimation(mBuilder.popupAnimation == null ? PopupAnimation.TranslateAlphaFromBottom : mBuilder.popupAnimation)
            .dismissOnTouchOutside(mBuilder.isCanDismiss)
            .autoOpenSoftInput(mBuilder.isAutoOpenSoftInput)
            .dismissOnBackPressed(mBuilder.isBackPressedDismiss)
            .asCustom(CustomBottomPopup(mBuilder))
            .show()
    }


//    /**
//     * 使用自动消失文字弹框
//     *
//     * @param msg       提示文字
//     * @param listener  消失回调
//     */
//    fun showAutoDismissDialog(msg: String?, listener: DialogOnlyComfirmListener) {
//        mBuilder.setCanDismiss(false)
//            .setBackPressedDismiss(false)
//            .setLayoutId(R.layout.dialog_toast_layout, object : DialogConvert() {
//                public override fun convertView(holder: ViewHolder, popupView: BasePopupView?) {
//                    val tvContent: TextView = holder.getView(R.id.tv_content)
//                    tvContent.setText(msg)
//                }
//            })
//
//        XPopup.Builder(mBuilder.context)
//            .dismissOnTouchOutside(mBuilder.isCanDismiss)
//            .dismissOnBackPressed(mBuilder.isBackPressedDismiss)
//            .autoOpenSoftInput(mBuilder.isAutoOpenSoftInput)
//            .asCustom(CustomPopup(mBuilder))
//            .show()
//            .delayDismissWith(1500, object : Runnable {
//                override fun run() {
//                    listener.comfirm()
//                }
//            })
//    }


    /**
     * 使用自动消失文字弹框
     *
     * @param dataList       提示文字
     * @param listener  消失回调
     */
    fun <T> showBottomListDialog(
        dataList: MutableList<T?>,
        listener: DialogListSelectListener<T>
    ): BasePopupView {
        val dataStr = arrayOfNulls<String>(dataList.size)


        for (i in dataList.indices) {
            var showName: String? = listener.getShowName(dataList.get(i) as T)
            if (TextUtils.isEmpty(showName)) {
                showName = ""
            }
            dataStr[i] = showName
        }

        return XPopup.Builder(mBuilder.context)
            .dismissOnTouchOutside(mBuilder.isCanDismiss)
            .dismissOnBackPressed(mBuilder.isBackPressedDismiss)
            .autoOpenSoftInput(mBuilder.isAutoOpenSoftInput)
            .maxHeight(
                mBuilder.context!!.getResources().getDimensionPixelOffset(R.dimen.dp750_500)
            )
            .asBottomList(
                mBuilder.title, dataStr,
                object : OnSelectListener {
                    public override fun onSelect(position: Int, text: String?) {
                        listener.onSelect(dataList.get(position) as T)
                    }
                })
//            .bindLayout(R.layout.item_bottom_adapter)
            .show()
    }

    fun <T> showCenterListDialog(
        dataList: MutableList<T?>,
        listener: DialogListSelectListener<T>
    ): BasePopupView {
        val showItems = arrayOfNulls<String>(dataList.size)
        for (i in dataList.indices) {
            showItems[i] = listener.getShowName(dataList.get(i) as T)
        }

        return XPopup.Builder(mBuilder.context)
            .dismissOnTouchOutside(mBuilder.isCanDismiss)
            .autoOpenSoftInput(mBuilder.isAutoOpenSoftInput)
            .dismissOnBackPressed(mBuilder.isBackPressedDismiss)
            .asCenterList(
                mBuilder.title, showItems,
                object : OnSelectListener {
                    public override fun onSelect(position: Int, text: String?) {
                        listener.onSelect(dataList.get(position) as T)
                    }
                })
            .show()
    }

    /**
     * 下拉菜单
     */
    fun <T> dropDownMenu(dataList: ArrayList<T?>, listener: DialogDropDownListener<T>): BasePopupView {
        val dataStr = arrayOfNulls<String>(dataList.size)


        for (i in dataList.indices) {
            dataStr[i] = listener.getShowName(dataList.get(i) as T)
        }

        return XPopup.Builder(mBuilder.context).asAttachList(
            dataStr, null,
            object : OnSelectListener {
                public override fun onSelect(position: Int, text: String?) {
                    listener.onSelect(dataList.get(position) as T)
                }
            })
            .show()
    }

    /**
     * 下拉菜单
     */
    fun <T> dropDownList(
        dataList: MutableList<T?>,
        view: View?,
        listener: DialogDropDownListener<T>
    ): BasePopupView {
        val dataStr = arrayOfNulls<String>(dataList.size)


        for (i in dataList.indices) {
            dataStr[i] = listener.getShowName(dataList.get(i) as T)
        }

        return XPopup.Builder(mBuilder.context)
            .hasShadowBg(false)
            .popupAnimation(PopupAnimation.ScrollAlphaFromTop) //NoAnimation表示禁用动画
            .isCenterHorizontal(true) //是否与目标水平居中对齐
            //                        .offsetY(-10)
            //                        .popupPosition(PopupPosition.Top) //手动指定弹窗的位置
            .atView(view) // 依附于所点击的View，内部会自动判断在上方或者下方显示
            //                .asCustom()
            .asAttachList(
                dataStr, null,
                object : OnSelectListener {
                    public override fun onSelect(position: Int, text: String?) {
                        listener.onSelect(dataList.get(position) as T)
                    }
                })
//            .bindItemLayout(R.layout.item_down_line_lay)
            .show()
    }

//    /**
//     * 下拉菜单
//     */
//    fun <T> dropDownList(
//        dataList: MutableList<T?>,
//        view: View?,
//        @LayoutRes itemRes: Int,
//        listener: DialogDropDownImgListener
//    ): BasePopupView {
//        val dataStr = arrayOfNulls<String>(dataList.size)
//        val imgSrcArr = IntArray(dataList.size)
//
//
//        for (i in dataList.indices) {
//            dataStr[i] = listener.getShowName(dataList.get(i))
//            imgSrcArr[i] = listener.getImgRes(dataList.get(i))
//        }
//
//        return XPopup.Builder(mBuilder.context)
//            .hasShadowBg(false)
//            .popupAnimation(PopupAnimation.ScrollAlphaFromTop) //NoAnimation表示禁用动画
//            .isCenterHorizontal(true) //是否与目标水平居中对齐
//            //                        .offsetY(-10)
//            //                        .popupPosition(PopupPosition.Top) //手动指定弹窗的位置
//            .atView(view) // 依附于所点击的View，内部会自动判断在上方或者下方显示
//            .asAttachList(
//                dataStr, imgSrcArr,
//                object : OnSelectListener {
//                    public override fun onSelect(position: Int, text: String?) {
//                        listener.onSelect(dataList.get(position))
//                    }
//                })
//            .bindItemLayout(itemRes)
//            .show()
//    }


//    /**
//     * 显示时间弹框
//     */
//    fun showDateDialog(listener: DateListener) {
//        showDateDialog(DateType.yyyyMMdd, listener, null, null)
//    }
//
//
//    /**
//     * 显示时间弹框
//     */
//    fun showDateAfterDialog(listener: DateListener) {
//        //系统当前时间
//
//        val startDate = Calendar.getInstance()
//        //        Calendar startDate = Calendar.getInstance();
////        startDate.set(2014, 1, 23);
//        val endDate = Calendar.getInstance()
//        endDate.set(2100, 1, 1)
//
//        showDateDialog(DateType.yyyyMMdd, listener, startDate, endDate)
//    }

//    /**
//     * 显示时间弹框
//     */
//    fun showDateDialog(
//        dateType: DateType,
//        listener: DateListener,
//        startDate: Calendar?,
//        endDate: Calendar?
//    ) {
//        var showType = booleanArrayOf(true, true, true, false, false, false)
//        if (dateType === DateType.yyyyMMdd) {
//            showType = booleanArrayOf(true, true, true, false, false, false)
//        } else if (dateType === DateType.yyyyMMddHHmm) {
//            showType = booleanArrayOf(true, true, true, true, true, false)
//        } else if (dateType === DateType.yyyyMMddHHmmss) {
//            showType = booleanArrayOf(true, true, true, true, true, true)
//        } else if (dateType === DateType.HHmm) {
//            showType = booleanArrayOf(false, false, false, true, true, false)
//        } else if (dateType === DateType.HHmmss) {
//            showType = booleanArrayOf(false, false, false, true, true, true)
//        }
//        val timePickerBuilder: TimePickerBuilder =
//            TimePickerBuilder(mBuilder.context, object : OnTimeSelectListener() {
//                public override fun onTimeSelect(date: Date?, v: View?) {
//                    val dateStr: String? = DateUtils.getDateStr(date, dateType.getDatetype())
//                    listener.select(dateStr)
//                }
//            }).setType(showType)
//        if (startDate != null && endDate != null) {
//            timePickerBuilder
//                .setRangDate(startDate, endDate)
//        }
//        val build: TimePickerView = timePickerBuilder
//            .build()
//        build.show()
//    }

    /**
     * 显示右侧侧拉框
     */
    fun showRightDrawer(popupView: BasePopupView?) {
        XPopup.Builder(mBuilder.context)
            .popupPosition(PopupPosition.Right) //右边
            .hasStatusBarShadow(true) //启用状态栏阴影
            .asCustom(popupView)
            .show()
    }

    companion object {
        fun getInstance(context: Context?): DialogBuilder {
            return DialogBuilder(context)
        }
    }
}